package com.gezi.admin.module.app.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gezi.admin.config.exception.type.ParamException;
import com.gezi.admin.module.app.user.model.AppUserSearchVo;
import com.gezi.admin.module.app.user.model.AppUserSexEnum;
import com.gezi.admin.module.app.user.model.AppUserUpdateInfoModel;
import com.gezi.admin.module.app.user.model.AppUserUpdatePhoneParamModel;
import com.gezi.admin.module.config.BaseController;
import com.gezi.admin.module.config.utils.PageUtil;
import com.gezi.admin.module.config.utils.PageVo;
import com.gezi.admin.security.SecurityUtil;
import com.gezi.admin.security.annotation.SecurityIgnoreUrl;
import com.gezi.admin.utils.wx.weapp.WxUtil;
import com.gezi.admin.utils.wx.weapp.model.UserPhoneModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author 格子代码
 * @createDate 2024/2/10 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/app/user")
public class AppUserController extends BaseController {

    @Resource
    private AppUserService appUserService;

    @ApiOperation("查询分页")
    @GetMapping(value = "/findAllByPage")
    public Object findAllByPage(PageVo page, AppUserSearchVo searchVo) {
        Page<LinkedHashMap> pageData = appUserService.findByPage(PageUtil.initMpPage(page),searchVo);
        for (LinkedHashMap record : pageData.getRecords()) {
            record.put("sexShow", AppUserSexEnum.valueOf(record.get("sex").toString()).getRemark());
        }
        return pageData;
    }

    @ApiOperation("查询用户信息")
    @GetMapping(value = "/findUserInfo")
    public Map findUserInfo() {
        String userId = SecurityUtil.getUserId();
        AppUserEntity appUserEntity = appUserService.getById(userId);
        Map userMap = MapUtil.newHashMap();
        BeanUtil.copyProperties(appUserEntity,userMap);
        userMap.put("sexShow", AppUserSexEnum.valueOf(appUserEntity.getSex()).getRemark());
        userMap.put("birthday", DateUtil.format(appUserEntity.getBirthday(),"yyyy-MM-dd"));
        userMap.put("createTime", DateUtil.format(appUserEntity.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
        return userMap;
    }

    @ApiOperation("修改信息")
    @PostMapping(value = "/editInfo")
    public void editInfo(@RequestBody AppUserUpdateInfoModel appUserUpdateInfoModel) {
        String userId = SecurityUtil.getUserId();
        AppUserEntity appUserEntity = appUserService.getById(userId);
        appUserEntity.setUpdateInfo(appUserUpdateInfoModel);
        appUserService.saveOrUpdate(appUserEntity);
    }

    @ApiOperation("修改手机")
    @SecurityIgnoreUrl
    @PostMapping(value = "/editPhone")
    public Object editPhone(@RequestBody @Validated AppUserUpdatePhoneParamModel paramModel) {

        String userId = SecurityUtil.getUserId();
        AppUserEntity appUserEntity = appUserService.getById(userId);
        //设置手机号
        UserPhoneModel userPhoneModel = WxUtil.getUserPhonenumber(paramModel.getCode());
        if (userPhoneModel.getErrcode() != 0) {
            throw ParamException.getInstance("手机号获取失败，请稍后重试");
        }
        appUserEntity.setPhone(userPhoneModel.getPhoneInfo().getPhoneNumber());
        appUserService.saveOrUpdate(appUserEntity);

        return appUserEntity.getPhone();
    }
}